package com.xoa.service.emergency.impl;

import com.xoa.dao.fixAssets.EduFixAssetsMapper;
import com.xoa.dao.hr.HrStaffInfoMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.dao.emergency.CpAssetDispatchMapper;
import com.xoa.model.emergency.CpAssetDispatch;
import com.xoa.service.emergency.CpAssetDispatchService;
import com.xoa.model.fixAssets.EduFixAssets;
import com.xoa.model.hr.HrStaffInfo;
import com.xoa.dao.emergency.CrashDispatchMapper;
import com.xoa.model.emergency.CrashDispatchWithBLOBs;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class CpAssetDispatchServiceImpl implements CpAssetDispatchService {

    @Resource
    HrStaffInfoMapper hrStaffInfoMapper;

    @Resource
    UsersMapper usersMapper;
    @Resource
    EduFixAssetsMapper eduFixAssetsMapper;

    @Resource
    CpAssetDispatchMapper cpAssetDispatchMapper;

    @Resource
    private CrashDispatchMapper crashDispatchMapper;

    @Override
    public List<CpAssetDispatch> selectAll(CpAssetDispatch cpAssetDispatch) {
        List<CpAssetDispatch> list = cpAssetDispatchMapper.selectAll(cpAssetDispatch);
        for(CpAssetDispatch cpAssetDispatch1 :list){
            if("1".equals(cpAssetDispatch1.getDispType()) && cpAssetDispatch1.getCoverUserId() != null){
                cpAssetDispatch1.setCoverUserName(usersMapper.getByUserId(cpAssetDispatch1.getCoverUserId()).getUserName());
            }
            CrashDispatchWithBLOBs crashDispatchWithBLOBs = crashDispatchMapper.selectByCrasId(cpAssetDispatch1.getCrasId());
            if(crashDispatchWithBLOBs != null ){
                cpAssetDispatch1.setCrasName(crashDispatchWithBLOBs.getPareEventName()+"-"+crashDispatchWithBLOBs.getEventName());
            }else{
                cpAssetDispatch1.setCrasName("救援事件已删除!");
            }

        }
        return list;
    }

    /**
     * 新增调度记录，并调度物资
     * @param cpAssetDispatch
     * @return
     */
    @Override
    @Transactional
    public int insertDispatch(CpAssetDispatch cpAssetDispatch) {
        boolean bol = false;
        cpAssetDispatch.setCreateTime(new Date());
        CrashDispatchWithBLOBs crashDispatchWithBLOBs = crashDispatchMapper.selectByPrimaryKey(cpAssetDispatch.getCoverCrasId());
        if("1".equals(cpAssetDispatch.getDispType())){
            HrStaffInfo hrStaffInfo = hrStaffInfoMapper.selectStaffInfo(cpAssetDispatch.getCoverUserId());
            if(hrStaffInfo != null && cpAssetDispatch.getCoverCrasId() != Integer.valueOf(hrStaffInfo.getProjectName())){
                hrStaffInfo.setProjectName(cpAssetDispatch.getCoverCrasId());
                hrStaffInfoMapper.updateByPrimaryKey(hrStaffInfo);
                bol=true;
            }
            if(hrStaffInfo != null && !crashDispatchWithBLOBs.getRescuePersons().contains(hrStaffInfo.getUserId()+",")){
                crashDispatchWithBLOBs.setRescuePersons(crashDispatchWithBLOBs.getRescuePersons()+hrStaffInfo.getUserId()+",");
                crashDispatchWithBLOBs.setRescuePersonsName(crashDispatchWithBLOBs.getRescuePersonsName()+hrStaffInfo.getStaffName()+",");
            }
        }else{
            EduFixAssets eduFixAssets = eduFixAssetsMapper.selectById(cpAssetDispatch.getCoverAssetId());
            if(eduFixAssets != null && cpAssetDispatch.getCoverCrasId() != Integer.valueOf(eduFixAssets.getOtherDept())){
                eduFixAssets.setOtherDept(String.valueOf(cpAssetDispatch.getCoverCrasId()));
                eduFixAssetsMapper.updateById(eduFixAssets);
                bol=true;
            }
            if(eduFixAssets != null && !crashDispatchWithBLOBs.getRescueEquipment().contains(eduFixAssets.getId()+",")){
                crashDispatchWithBLOBs.setRescueEquipment(crashDispatchWithBLOBs.getRescueEquipment()+eduFixAssets.getId()+",");
                crashDispatchWithBLOBs.setRescueEquipmentName(crashDispatchWithBLOBs.getRescueEquipmentName()+eduFixAssets.getAssetsName()+",");
            }
        }
//        int x = crashDispatchMapper.updateByPrimaryKeyAdd(crashDispatchWithBLOBs);
        int x = crashDispatchMapper.updateByPrimaryKeyWithBLOBs(crashDispatchWithBLOBs);
        if(cpAssetDispatch.getCrasId() != null && cpAssetDispatch.getCrasId() != 0 && bol){
            cpAssetDispatchMapper.insertSelective(cpAssetDispatch);
            if(x<=0){
                cpAssetDispatchMapper.deleteByPrimaryKey(cpAssetDispatch.getDispId());
            }
        }
        return x;
    }

    /**
     * 批量调度设备人员
     * @return
     */
    @Override
    public int addsDispatch(CrashDispatchWithBLOBs crashDispatchWithBLOBs,String userId) {

        //修改应急人员时 判断人员是否属于某一个 任务 如果属于  生成调拨记录

        if(crashDispatchWithBLOBs.getRescuePersons()!= null && !"".equals(crashDispatchWithBLOBs.getRescuePersons())){
            String [] strUsers = crashDispatchWithBLOBs.getRescuePersons().split(",");
            for(String str :strUsers){
//                HrStaffInfo hrStaffInfo = hrStaffInfoMapper.queryHrByUsername(str,crashDispatchWithBLOBs.getCrasId());
                HrStaffInfo  hrStaffInfo = hrStaffInfoMapper.selectStaffInfo(str);
                if(hrStaffInfo != null){
                    if( hrStaffInfo.getProjectName() != null && crashDispatchWithBLOBs.getCrasId() != Integer.valueOf(hrStaffInfo.getProjectName())){
                        CpAssetDispatch cpAssetDispatch = new CpAssetDispatch();
                        cpAssetDispatch.setDispUserId(userId);
                        cpAssetDispatch.setCreateTime(new Date());
                        cpAssetDispatch.setDispType("1");
                        cpAssetDispatch.setCoverUserId(hrStaffInfo.getUserId());
                        cpAssetDispatch.setCrasId(hrStaffInfo.getProjectName());
                        cpAssetDispatch.setCoverCrasId(crashDispatchWithBLOBs.getCrasId());
                        //生成调度记录
                        cpAssetDispatchMapper.insertSelective(cpAssetDispatch);
                    }else{
                        hrStaffInfo.setProjectName(crashDispatchWithBLOBs.getCrasId());
                        //修改所属任务
                        hrStaffInfoMapper.updateByPrimaryKey(hrStaffInfo);
                    }
                }
            }
        }

        //修改任务物资时 判断物资是否属于某一个 任务 如果属于  生成调拨记录  并更换所属任务
        if(crashDispatchWithBLOBs.getRescueEquipment() != null && !"".equals(crashDispatchWithBLOBs.getRescueEquipment())){
            String [] strings = crashDispatchWithBLOBs.getRescueEquipment().split(",");
            for(String str :strings){
                EduFixAssets eduFixAssets = eduFixAssetsMapper.selectById(str);
                if(eduFixAssets != null && eduFixAssets.getOtherDept() != null && !"".equals(eduFixAssets.getOtherDept().trim()) && crashDispatchWithBLOBs.getCrasId()!=Integer.valueOf(eduFixAssets.getOtherDept())){
                    CpAssetDispatch cpAssetDispatch = new CpAssetDispatch();
                    cpAssetDispatch.setDispUserId(userId);
                    cpAssetDispatch.setCreateTime(new Date());
                    cpAssetDispatch.setDispType("2");
                    cpAssetDispatch.setCoverAssetId(eduFixAssets.getId());
                    cpAssetDispatch.setCrasId(Integer.valueOf(eduFixAssets.getOtherDept()));
                    cpAssetDispatch.setCoverCrasId(crashDispatchWithBLOBs.getCrasId());
                    //生成调度记录
                    cpAssetDispatchMapper.insertSelective(cpAssetDispatch);
                }
                eduFixAssets.setOtherDept(String.valueOf(crashDispatchWithBLOBs.getCrasId()));
                //修改所属任务
                eduFixAssetsMapper.updateById(eduFixAssets);
            }
        }
        return 0;
    }
}
